package com.heima.minio.service;

import com.heima.minio.config.MinIOProperties;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.heima.minio.service
 * @Author: wangjian
 * @CreateTime: 2022-06-30  21:08
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class MinIOService {
    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinIOProperties properties;

/**
 * @description: 上传文件
 * @author: wangjian
        * @date: 2022/6/30 21:53
 * @param: file
 * @return: java.lang.String
 **/
    public String upload(MultipartFile file) {
        //原文件名
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构造新的文件名,名字不重复
       String xinName =  UUID.randomUUID().toString()+substring;
  //上传文件
        try {
            PutObjectArgs.builder().contentType(file.getContentType()).
                    stream(file.getInputStream(),file.getSize(),-1)
                     .bucket(properties.getBucketName())
                    .object(xinName).build();
        } catch (IOException e) {
            e.printStackTrace();
            throw  new  RuntimeException("上传文件失败:" +  e.getMessage());
        }
        return properties.getEndpoint()+"/"+properties.getBucketName()+"/"
                +xinName;
    }
/**
 * @description: 删除文件
 * @author: wangjian
        * @date: 2022/6/30 21:54
 * @param: url
 **/
    public void delete(String url) {
        String objectName = url.replace(properties.getEndpoint()+"/","")
                .replace(properties.getBucketName()+"/","");
        RemoveObjectArgs args = RemoveObjectArgs.builder()
                .bucket(properties.getBucketName())
                .object(objectName)
                .build();
        try {
            minioClient.removeObject(args);
        } catch (Exception e) {
            throw new RuntimeException("删除文件失败: " + e.getMessage());
        }
    }
    /**
     * 下载文件
     * @param url
     * @return
     */
    public InputStream download(String url){
        String objectName = url.replace(properties.getEndpoint()+"/","")
                .replace(properties.getBucketName()+"/","");
        GetObjectArgs args = GetObjectArgs.builder()
                .bucket(properties.getBucketName())
                .object(objectName)
                .build();
        InputStream inputStream = null;
        try {
            inputStream = minioClient.getObject(args);
        } catch (Exception e) {
            throw new RuntimeException("下载文件失败: " + e.getMessage());
        }
        return inputStream;
    }
    }

